package com.effective.java.generics;

import java.util.Iterator;
import java.util.List;

/**
 * Class offering capabilities in order to determine the MAX value of a list of
 * values.
 * 
 * @author andreas.landerer
 * 
 */
public class GenericMax {

	/**
	 * Method returning the MAX value of a list of values
	 * 
	 * @param list
	 *            list of values
	 * @return MAX value
	 */
	public static <T extends Comparable<? super T>> T max(final List<? extends T> list) {

		if (list == null) {
			throw new IllegalArgumentException("Mandatory input parameter 'list' is NULL!");
		}

		Iterator<? extends T> it = list.iterator();
		T result = it.next();
		while (it.hasNext()) {
			T nextResult = it.next();
			if (nextResult.compareTo(result) > 0) {
				result = nextResult;
			}
		}

		return result;
	}
}
